import Util from "../../utils/util";
import {getActivityList} from "../../api/activity";
import {getBannerData} from "../../api/api";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    parameter: {
      title: "欢迎来到此面向上"
    },

    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    filterFields: {},
    filterDialog: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;

    this.setData({
      bottom: 30
    })

    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo || null
      })
      getBannerData("slides").then(res => {
        this.setData({...res.data})
      })
      that.getLists();
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  closeFilterDialog(e) {
    this.setData({filterDialog: false})
  },

  showFilter(e) {
    this.setData({
      filterDialog: true,
    })
  },

  resetFilter() { //筛选重置

    this.setData({
      filter: null,
      loading: false, // 是否加载中
      loadend: false, // 是否加载完毕
      loadTitle: '加载更多内容', // 提示语
      lists: [],
      page: 1,
      et_show: true,
    })

    this.getLists();
  },

  confirmFilter(e) {
    this.setData({
      // filterDialog: false,
      filter: e.detail,
      loading: false, // 是否加载中
      loadend: false, // 是否加载完毕
      loadTitle: '加载更多内容', // 提示语
      lists: [],
      page: 1,
      et_show: true,
    })
    this.getLists();
  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getActivityList(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;
      lists = app.SplitArray(list, lists);

      for (const k in lists) {
        if (lists[k].score <= 0) continue;
        let nums = [];
        for (let i = 0; i < parseInt(lists[k].score || 0); i++) {
          nums.push(i)
        }
        lists[k].score_nums = nums;
      }
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})